package com.lss.myapplication.db.database;

import android.content.Context;

import androidx.room.Database;
import androidx.room.Room;
import androidx.room.RoomDatabase;

import com.lss.myapplication.db.City;
import com.lss.myapplication.db.County;
import com.lss.myapplication.db.Province;
import com.lss.myapplication.db.dao.CityDao;
import com.lss.myapplication.db.dao.CountyDao;
import com.lss.myapplication.db.dao.ProvinceDao;

@Database(entities = {Province.class, City.class, County.class}, version = 1, exportSchema = false)

public abstract class AppDatabase extends RoomDatabase {

    private static AppDatabase instance;

    public abstract ProvinceDao provinceDao();

    public abstract CityDao cityDao();
    public abstract CountyDao countyDao();

    private static final String DATABASE_NAME = "weather_database";

    public static synchronized AppDatabase getInstance(Context context) {
        if (instance == null) {
            instance = Room.databaseBuilder(context.getApplicationContext(),
                    AppDatabase.class, DATABASE_NAME)
                    .fallbackToDestructiveMigration() // 升级异常时重建数据库
                    .build();
        }
        return instance;
    }
}
